package com.sunyard.manage.config;


import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author 张昊坤
 * @Description TODO
 * @Date 2021/1/19 11:04
 */

@Configuration
// @AutoConfigureAfter(ShiroLifecycleBeanPostProcessorConfig.class)
public class ShiroConfig {

//    @Autowired
//    private PopedomMapper popedomMapper;

    @Bean
    public SecurityManager securityManager(MyShiroRealm realm) {
        DefaultWebSecurityManager sManager = new DefaultWebSecurityManager();
        sManager.setRealm(realm);
        //关闭shiro自带的session
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        sManager.setSubjectDAO(subjectDAO);
        return sManager;

    }

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactory(SecurityManager securityManager) {
        ShiroFilterFactoryBean sfBean = new ShiroFilterFactoryBean();
        Map<String, Filter> filterMap = new LinkedHashMap<>();
        filterMap.put("jwt", new JWTFilter());
        sfBean.setFilters(filterMap);
        sfBean.setSecurityManager(securityManager);
        LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/*.html", "anon");
        filterChainDefinitionMap.put("/login/login", "anon");
        filterChainDefinitionMap.put("/ping/f5check", "anon");
        filterChainDefinitionMap.put("/login/doorLogin", "anon");
        filterChainDefinitionMap.put("/finger/fingerConvert", "anon");
        filterChainDefinitionMap.put("/login/handLogin", "anon");
        filterChainDefinitionMap.put("/login/moaLogin", "anon");
        filterChainDefinitionMap.put("/login", "anon");
        filterChainDefinitionMap.put("/treminal/getData", "anon");
        filterChainDefinitionMap.put("/login/getOperatorInfo", "anon");
        filterChainDefinitionMap.put("/popedom/findPopedoms", "anon");
        filterChainDefinitionMap.put("/login/logout", "anon");
        filterChainDefinitionMap.put("/token/refreshToken", "anon");
        filterChainDefinitionMap.put("/error", "anon");
        filterChainDefinitionMap.put("/*.ico", "anon");
        filterChainDefinitionMap.put("/*.json", "anon");
        filterChainDefinitionMap.put("/geoJsonColl/china.json", "anon");
        filterChainDefinitionMap.put("/geoJsonColl/**", "anon");
        filterChainDefinitionMap.put("/theme/**", "anon");
        filterChainDefinitionMap.put("/swagger-resources/**", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        filterChainDefinitionMap.put("/services", "anon");
        filterChainDefinitionMap.put("/organization/getOrgIds", "anon");
        filterChainDefinitionMap.put("/organization/newOrgTree", "anon");
        filterChainDefinitionMap.put("/esbManage/*", "anon");
        filterChainDefinitionMap.put("/actuator/*", "anon");
        filterChainDefinitionMap.put("/actuator", "anon");
        filterChainDefinitionMap.put("/merchantqy/qy", "anon");
        filterChainDefinitionMap.put("/login/getVerify", "anon");
        filterChainDefinitionMap.put("/merchantqy/qyClQrCode", "anon");
        filterChainDefinitionMap.put("/generalInfo/uploadModelFile", "anon");
        filterChainDefinitionMap.put("/blacklist/errorDownloads", "anon");
        filterChainDefinitionMap.put("/merchantReAudit/merchantWxModify", "anon");
        filterChainDefinitionMap.put("/merchantReAudit/merchantAliModify", "anon");
        filterChainDefinitionMap.put("/merchantDevice/merchantWxBind", "anon");
        filterChainDefinitionMap.put("/merchantDevice/merchantAliBind", "anon");
        filterChainDefinitionMap.put("/**", "jwt");
        sfBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return sfBean;
    }

   /* @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        // 强制使用cglib，防止重复代理和可能引起代理出错的问题
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }*/

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }



   /* @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }*/

}
